
import Request from "@/utils/http.js"
import {BASE_PATH} from "@/utils/config.js"
import {getStorage} from "@/utils/common.js"

export const login = (params,callback)=>{ // 
	return new Promise((resolve,reject) =>{
		uni.login({
		  provider: 'weixin', //使用微信登录
		  success: function (loginRes) {
		    Request({
		    	url: '/base/login',
		    	method:'POST',
		    	data: {
					type:3,
					code:loginRes.code
				},
		    	success: res => {
		    		resolve(res)
		    		callback && callback(res);
		    	},
		    	fail: res => {
		    		resolve(res)
		    		callback && callback(res);
		    	}
		    })
		  }
		});
		
	})
}

export const getCategoryList = (params,callback)=>{ // 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/category/getCategoryList',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const getNewsList = (params,callback)=>{ // 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/news/getNewsList',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const getServiceList = (params,callback)=>{ // 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/service/getServiceList',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const getGoodsList = (params,callback)=>{ // 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/goods/getGoodsList',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const getServiceStationList = (params,callback)=>{ // 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/station/getServiceStationList',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const createAccompanyOrder = (params,callback)=>{ // 乐龄陪伴创建订单（非居家互助）
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/service/createServiceAccompanyOrder',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const createAidOrder = (params,callback)=>{ // 居家互助创建订单
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/service/createServiceAidOrder',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}
export const saveMemberInfo = (params,callback)=>{ 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/member/saveMemberInfo',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const getShippingCost = (params,callback)=>{ 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/service/getShippingCost',
			method:'GET',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const createGoodsOrder = (params,callback)=>{ 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/goods/createGoodsOrder',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const createServiceOrder = (params,callback)=>{ 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/service/createServiceOrder',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const uploadImage = ()=>{
	return new Promise((resolve,reject)=>{
		uni.chooseImage({
			success: (chooseImageRes) => {
				uni.showLoading({
					title: '文件上传中...'
				})
				const tempFilePaths = chooseImageRes.tempFilePaths;
				uni.uploadFile({
					url:`${BASE_PATH}/mini/file/saveFile`,
					filePath: tempFilePaths[0], // 
					name: 'file',
					formData:{
						bucket:'service'
					},
					header:{
						"token":getStorage('token') || ''
					},
					success: (uploadFileRes) => {
						uni.hideLoading();
						try {
							if(!uploadFileRes) throw Error('kong')
							let data = JSON.parse(uploadFileRes.data)
							resolve(data)
							
						} catch (e) {
							resolve(e)
						}
					
					},
					fail: () => {
						uni.hideLoading();
						uni.showModal({
							showCancel: false,
							title: '上传失败'
						})
					},
					complete() {
						
					}
				});
			}
		});
	})
}

export const getGoodsOrderList = (params,callback)=>{ 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/member/getGoodsOrderList',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const getServiceOrderList = (params,callback)=>{ 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/member/getServiceOrderList',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const getServiceAidOrderList = (params,callback)=>{ 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/member/getServiceAidOrderList',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const getPointRecordList = (params,callback)=>{ 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/member/getPointRecordList',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const getMemberInfo = (params,callback)=>{ 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/member/getMemberInfo',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}

export const aidOrderUploadImg = (params,callback)=>{ 
	return new Promise((resolve,reject) =>{
		Request({
			url: '/mini/member/saveVolunteerServiceAidOrder',
			method:'POST',
			data: params,
			success: res => {
				resolve(res)
				callback && callback(res);
			},
			fail: res => {
				resolve(res)
				callback && callback(res);
			}
		})
	})
}